﻿'         This file is Copyright © 2008-2012 Billy O'Neal and Aommaster
'                            All rights reserved.
'
' This file is released under the 2-Clause BSD license, included in LICENSE.TXT
'                           and on the MHWT website
Friend Class HexConvert

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim tempstring As String = ""
        Dim inputstring As String = inputbox.Text
        Dim linearray As Array = Nothing
        Dim charray() As Char = Nothing
        Dim i As Integer = 0

        Try
            Select Case modebox.Text
                Case "ASCII to Hex(2) for REGEDIT4"
                    linearray = inputstring.Split(CChar(Environment.NewLine))
                    For Each line As String In linearray
                        i = 0
                        line = line.Trim
                        charray = line.ToCharArray
                        For Each c As Char In charray
                            tempstring += AscW(c).ToString("X").ToString + ","
                            i += 1
                            If i <> 0 And i Mod 12 = 0 Then
                                tempstring += "\" + Environment.NewLine + "  "
                            End If
                        Next
                        tempstring += "00" + Environment.NewLine
                    Next
                Case "ASCII to Hex(2) for Windows Registry Editor v.5.0"
                    linearray = inputstring.Split(CChar(Environment.NewLine))
                    For Each line As String In linearray
                        i = 0
                        line = line.Trim
                        charray = line.ToCharArray
                        For Each c As Char In charray
                            tempstring += AscW(c).ToString("X").ToString + ",00,"
                            i += 2
                            If i <> 0 And i Mod 20 = 0 Then
                                tempstring += "\" + Environment.NewLine + "  "
                            End If
                        Next
                        tempstring += "00" + Environment.NewLine
                    Next
                Case "ASCII to Hex(7) for REGEDIT4"
                    linearray = inputstring.Split(CChar(Environment.NewLine))
                    For Each line As String In linearray
                        line = line.Trim
                        charray = line.ToCharArray
                        For Each c As Char In charray
                            tempstring += AscW(c).ToString("X").ToString + ","
                            i += 1
                            If i <> 0 And i Mod 12 = 0 Then
                                tempstring += "\" + Environment.NewLine + "  "
                            End If
                        Next
                        tempstring += "00,"
                        i += 1
                        If i <> 0 And i Mod 12 = 0 Then
                            tempstring += "\" + Environment.NewLine + "  "
                        End If
                    Next
                    tempstring += "00"
                Case "ASCII to Hex(7) for Windows Registry Editor v.5.0"
                    linearray = inputstring.Split(CChar(Environment.NewLine))
                    For Each line As String In linearray
                        line = line.Trim
                        charray = line.ToCharArray
                        For Each c As Char In charray
                            tempstring += AscW(c).ToString("X").ToString + ",00,"
                            i += 2
                            If i <> 0 And i Mod 12 = 0 Then
                                tempstring += "\" + Environment.NewLine + "  "
                            End If
                        Next
                        tempstring += "00,00,"
                        i += 2
                        If i <> 0 And i Mod 12 = 0 Then
                            tempstring += "\" + Environment.NewLine + "  "
                        End If
                    Next
                    tempstring += "00,00"
                Case "Hex(2) to ASCII from REGEDIT4"
                    linearray = inputstring.Replace(Environment.NewLine, ",").Replace("\", ",").Split(CChar(","))
                    For Each c As String In linearray
                        If c.Trim = "00" Then
                            tempstring += Environment.NewLine
                        ElseIf c.Trim <> "" Then
                            tempstring += ChrW(CInt(Convert.ToInt64(c.Trim, 16))).ToString()
                        End If
                    Next
                Case "Hex(2) to ASCII from Windows Registry Editor v.5.0"
                    inputstring = inputstring.Replace(Environment.NewLine, ",").Replace("\", ",")
                    inputstring = inputstring.Replace(",00,00,00", ",[newline]")
                    inputstring = inputstring.Replace(",00", "").Replace("[newline]", "00")
                    linearray = inputstring.Split(CChar(","))
                    For Each c As String In linearray
                        If c.Trim = "00" Then
                            tempstring += Environment.NewLine
                        ElseIf c.Trim <> "" Then
                            tempstring += ChrW(CInt(Convert.ToInt64(c.Trim, 16))).ToString()
                        End If
                    Next
                Case "Hex(7) to ASCII from REGEDIT4"
                    linearray = inputstring.Replace(Environment.NewLine, ",").Replace("\", ",").Split(CChar(","))
                    For Each c As String In linearray
                        If c.Trim = "00" Then
                            tempstring += Environment.NewLine
                        ElseIf c.Trim <> "" Then
                            tempstring += ChrW(CInt(Convert.ToInt64(c.Trim, 16))).ToString()
                        End If
                    Next
                Case "Hex(7) to ASCII from Windows Registry Editor v.5.0"
                    inputstring = inputstring.Replace(Environment.NewLine, ",").Replace("\", ",")
                    inputstring = inputstring.Replace(",00,00,00", ",[newline]")
                    inputstring = inputstring.Replace(",00", "").Replace("[newline]", "00")
                    linearray = inputstring.Split(CChar(","))
                    For Each c As String In linearray
                        If c.Trim = "00" Then
                            tempstring += Environment.NewLine
                        ElseIf c.Trim <> "" Then
                            tempstring += ChrW(CInt(Convert.ToInt64(c.Trim, 16))).ToString()
                        End If
                    Next
            End Select
            outputbox.Text = tempstring
        Catch ex As Exception
            MsgBox("Invalid input. Please make sure that you are converting between the right formats")
        End Try


    End Sub
End Class